/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By 深圳中科鑫智科技有限公司
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.app.controller.wallet;

import cn.hutool.core.bean.BeanUtil;
import com.shop.cereshop.app.domain.wallet.UserIncomeAndExpenseDTO;
import com.shop.cereshop.app.param.wallet.WalletTransactionQueryVO;
import com.shop.cereshop.app.param.wallet.WalletTransactionVO;
import com.shop.cereshop.app.service.wallet.BuyerWalletService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/wallet/transaction")
@Api(tags = "钱包流水查询接口")
public class WalletTransactionController {

    @Autowired
    private BuyerWalletService buyerWalletService;

    @ApiOperation("查询钱包流水列表")
    @GetMapping("/list")
    public Result<Map<String, Object>> getTransactionList(WalletTransactionQueryVO queryVO, HttpServletRequest request) {
        try {
            //获取当前登录账户
            CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
            if (BeanUtil.isEmpty(user)) {
                return Result.failed(CoReturnFormat.USER_NOT_LOGIN);
            }
            Map<String, Object> result = new HashMap<>(8);
            List<WalletTransactionVO> transactionList = buyerWalletService.getTransactionList(user.getBuyerUserId(), queryVO);
            result.put("transactionList", transactionList);

//            if (CollUtil.isNotEmpty(transactionList)) {
//                double totalExpend = 0.00;
//                double totalIncome = 0.00;
//                for (WalletTransactionVO walletTransactionVO : transactionList) {
//                    if (Objects.equals(walletTransactionVO.getTransactionType(), "消费") || Objects.equals(walletTransactionVO.getTransactionType(), "提现")) {
//                        totalExpend += walletTransactionVO.getAmount().doubleValue();
//                        // 将Amount修改为负值
//                        walletTransactionVO.setAmount(walletTransactionVO.getAmount().multiply(new java.math.BigDecimal(-1)));
//                    } else {
//                        totalIncome += walletTransactionVO.getAmount().doubleValue();
//                    }
//                }
//                //总支出
//                result.put("totalExpend", totalExpend);
//                //总收入
//                result.put("totalIncome", totalIncome);
//            }

            // 根据起始时间 获取用户收入和支出
            UserIncomeAndExpenseDTO dto = buyerWalletService.getUserIncomeAndExpense(user.getBuyerUserId(), queryVO.getStartDate(), queryVO.getEndDate());
            double defaultAmount = 0d;
            result.put("totalIncome", dto != null ? dto.getTotalIncome() : defaultAmount);
            result.put("totalExpend", dto != null ? dto.getTotalExpend() : defaultAmount);

            return Result.success(result);
        } catch (Exception e) {
            log.error("查询钱包流水失败", e);
            return Result.failed(CoReturnFormat.WALLET_TRANSACTION_LIST_FAIL);
        }
    }

    @ApiOperation("查询钱包流水详情")
    @GetMapping("/detail")
    public Result<WalletTransactionVO> getTransactionDetail(@RequestParam Long transactionId, HttpServletRequest request) {
        try {
            //获取当前登录账户
            CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
            if (BeanUtil.isEmpty(user)) {
                return Result.failed(CoReturnFormat.USER_NOT_LOGIN);
            }

            WalletTransactionVO transactionDetail = buyerWalletService.getTransactionDetail(transactionId);
            if (transactionDetail != null) {
                return Result.success(transactionDetail);
            } else {
                return Result.failed(CoReturnFormat.WALLET_TRANSACTION_DETAIL_FAIL);
            }
        } catch (Exception e) {
            log.error("查询钱包流水详情失败", e);
            return Result.failed(CoReturnFormat.WALLET_TRANSACTION_DETAIL_FAIL);
        }
    }
}
